package com.htudata.core.util.operation;

import java.lang.reflect.Method;
public class ClassOperationUtils {

  private static final String GETTER = "get";
  
  private static final String SETTER = "set";
  
  private static final String ORIGIN_GETTER_NAME = "getClass";
  

  public Object getNewInstance(String className)  throws Exception  {
    try {
      Class<?> tempClass = Class.forName(className);
      Object result = tempClass.newInstance();
      return result;
    } catch (Exception e) {
      throw e;
    }
  }
  
  public static void setOriginValueOrDefaultValue(Object obj, String defaultValue)  throws Exception {
    Method[] methodArr = obj.getClass().getMethods();
    String field = "";
    Method getter = null;
    Method setter = null;
    Object value = null;
    byte b;
    int i;
    Method[] arrayOfMethod1;
    for (i = (arrayOfMethod1 = methodArr).length, b = 0; b < i; ) {
      Method method = arrayOfMethod1[b];
      if (isGetter(method)) {
        field = getFieldOfGetterOrSetter(method.getName());
        getter = getGetter(obj, field, new Class[0]);
        setter = getSetter(obj, field, String.class);
        value = executeMethod(obj, getter, new Object[0]);
        value = StringOperationUtils.getValueOrReturnDefaultValue((String)value, defaultValue);
        executeMethod(obj, setter, new Object[] { value });
      } 
      b++;
    } 
  }
  
  public static String getFieldOfGetterOrSetter(String methodName) {
    String result = methodName.replace("get", "");
    result = result.replace("set", "");
    return result;
  }
  
  public static <T> Object executeGetter(Object obj, String fieldName, Class... paramClass) throws Exception  {
    return executeMethod(obj, getGetter(obj, fieldName, new Class[0]), new Object[0]);
  }
  
  public static <T> Object executeSetter(Object obj, String fieldName, Class<T> paramClass, Object... paramValue) throws Exception  {
    return executeMethod(obj, getSetter(obj, fieldName, paramClass), paramValue);
  }
  
  public static Object executeMethod(Object obj, Method method, Object... paramValue) throws Exception {
    if (obj == null)
      return null; 
    if (method == null)
      return null; 
    try {
      Object result = method.invoke(obj, paramValue);
      return result;
    } catch (Exception e) {
      throw e;
    }
  }
  
  public static <T> Method getGetter(Object obj, String fieldName, Class... paramClass)  throws Exception  {
    return getMethod(obj, getGetterName(fieldName), paramClass);
  }
  
  public static <T> Method getSetter(Object obj, String fieldName, Class<T> paramClass)  throws Exception  {
    return getMethod(obj, getSetterName(fieldName), new Class[] { paramClass });
  }
  
  public static <T> Method getMethod(Object obj, String methodName, Class... paramClass)  throws Exception  {
    if (obj == null)
      return null; 
    try {
      Method result = obj.getClass().getMethod(methodName, paramClass);
      return result;
    } catch (Exception e) {
      throw e;
    }
  }
  
  public static String getGetterName(String fieldName) {
    return getMethodNameOfGetterOrSetter("get", fieldName);
  }
  
  public static String getSetterName(String fieldName) {
    return getMethodNameOfGetterOrSetter("set", fieldName);
  }
  
  public static String getMethodNameOfGetterOrSetter(String methodType, String fieldName) {
    String firstLetterUpperCaseOfField = fieldName.toUpperCase().substring(0, 1);
    String result = String.valueOf(methodType) + firstLetterUpperCaseOfField + fieldName.substring(1);
    return result;
  }
  
  public static boolean isGetter(Method method) {
    if (method.getName().startsWith("get") && 
      !"getClass".equals(method.getName()))
      return true; 
    return false;
  }
  
  public static boolean isSetter(Method method) {
    if (method.getName().startsWith("set"))
      return true; 
    return false;
  }
}
